package com.atguigu.gulimall.seckill.Controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.vo.SeckillRedisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SeckillController {
    @Autowired
    private SeckillService seckillService;
    //获取当前秒杀的所有商品
    @GetMapping("/getCurrentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SeckillRedisVo> seckillRedisVoList = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(seckillRedisVoList);
    }
    //根据skuId查询当前商品的秒杀信息
    @GetMapping(value = "/getSeckillWithSkuid")
    @ResponseBody
    public R getSeckillWithSkuid(@RequestParam Long skuId){
        SeckillRedisVo seckillRedisVo = seckillService.getSeckillWithSkuid(skuId);
        return R.ok().setData(seckillRedisVo);
    }

    @GetMapping("/kill")
    public String kill(String killId,String key,Integer num,Model model){
        //检查是否登录
        try {
            String orderSn = seckillService.kill(killId,key,num);
            model.addAttribute("orderSn",orderSn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

}
